//
//  XiaoKaiTypeViewController.m
//  Project_A
//
//  Created by 凯 on 15/7/17.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiTypeViewController.h"
#import "TypeHeaderView.h"
#import "UIImageView+WebCache.h"
#import "XiaoKaiSearchViewController.h"
#import "ClassifyViewController.h"
@interface XiaoKaiTypeViewController ()<UITableViewDelegate , UITableViewDataSource, UICollectionViewDelegate ,UICollectionViewDataSource>

@property (nonatomic ,retain)UIButton * sectionButton;
@property (nonatomic ,assign) NSInteger numOfSection;

@property (nonatomic ,retain)NSMutableArray * array;
@property (nonatomic ,retain)NSMutableArray * array2;
@property (nonatomic ,retain)NSMutableArray * deleteOtherCellArr;
@property(nonatomic, assign) NSInteger tempNum;



@property (nonatomic ,retain)NSMutableArray * pictureArr;

@end

@implementation XiaoKaiTypeViewController
- (void)getData
{
    [TypeModel getDataWithBlovk:^(NSMutableArray *array1, NSMutableArray *array2, NSMutableArray *array3, NSMutableArray *array4) {
        self.dataSources1 = [NSMutableArray array];
        self.dataSources2 = [NSMutableArray array];
        self.dataSources3 = [NSMutableArray array];
        self.dataSources4 = [NSMutableArray array];

       self.dataSources1 = array1;
       self.dataSources2 = array2;
        self.dataSources3 = array3;
      self.dataSources4 = array4;
       
       [[NSNotificationCenter defaultCenter]postNotificationName:@"creloda" object:nil];
        [self.tableView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.navigationController.navigationBarHidden = YES;
    self.array = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:1],[NSNumber numberWithInteger:2], [NSNumber numberWithInteger:3] ,nil];
    self.array2 = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInteger:0], [NSNumber numberWithInteger:1],[NSNumber numberWithInteger:2], [NSNumber numberWithInteger:3], nil];
    
    self.pictureArr = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"t1"], [UIImage imageNamed:@"t2"], [UIImage imageNamed:@"t3"] , [UIImage imageNamed:@"t4"] ,nil];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 44) style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
    [button setBackgroundImage:[UIImage imageNamed:@"t5"] forState:UIControlStateNormal];
    self.tableView.tableFooterView = button;
    [button addTarget:self action:@selector(pinpai) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.tableView];
    [self creatNav];

    
     // Do any additional setup after loading the view, typically from a nib.
}
-(void)pinpai
{
    ClassifyViewController *class = [[[ClassifyViewController alloc]init]autorelease];
    class.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:class animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSNumber * num = [NSNumber numberWithInteger:section];
    if ([self.array containsObject:num]) {
        return 0;
    }
    else
    {
        return 1;
    }
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    TypeHeaderView * headView = [[TypeHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
     headView.sectionButton.tag = section;
    [headView.sectionButton addTarget:self action:@selector(sectionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    headView.imageView.image = _pictureArr[section];
    

    return headView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (void)sectionButtonAction:(UIButton *)sender
{
    // 给numOfSection 赋值
    _numOfSection = sender.tag ;
    
    // num用于标记  检查section是否被点击
    NSNumber *num = [NSNumber numberWithInteger:sender.tag ];
    
    NSMutableArray * indexPathArray = [[NSMutableArray alloc]init];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:sender.tag ];
    [indexPathArray addObject:indexPath];
    
    // 判断是展开还是关闭
    if ([_array containsObject:num]) {
        // 判断是否已经有section 有插入的row了
        if (_array.count != _array2.count) {
            // 如果有
            _deleteOtherCellArr = [NSMutableArray array];
            NSIndexPath *tempIndexPath = [NSIndexPath indexPathForItem:0 inSection:_tempNum];
            [_deleteOtherCellArr addObject:tempIndexPath];
            NSNumber *tempNum = [NSNumber numberWithInteger:_tempNum];
            [_array removeObject:num];
            [_tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
            [_array addObject:tempNum];
            
            [_tableView deleteRowsAtIndexPaths:_deleteOtherCellArr withRowAnimation:UITableViewRowAnimationAutomatic];
        } else {
            // 如果只是section中没有打开的  走此处  插入row
            [_array removeObject:num];
            [_tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    } else {
        // 删除点击的section中的row
        [_array addObject:num];
        [_tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    //        将当前点击的section数存入_tempNum
    _tempNum = [num integerValue];
    
    
    
    
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_numOfSection == 0) {
        
        static NSString *identifier = @"secondcell";
        TypeTableViewCell * scell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!scell) {
            scell = [[TypeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            scell.collectionView.dataSource = self;
            scell.collectionView.delegate = self;
            
        }
        return  scell;
    }
    else if (_numOfSection ==1)
    {
        static NSString *identifier = @"secondcell2";
        TypeTableViewCell * scell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!scell) {
            scell = [[TypeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            scell.collectionView.dataSource = self;
            scell.collectionView.delegate = self;
        }
        return  scell;
    }
    else if (_numOfSection == 2)
    {
        static NSString *identifier = @"secondcell3";
        TypeTableViewCell * scell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!scell) {
            scell = [[TypeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            scell.collectionView.dataSource = self;
            scell.collectionView.delegate = self;
        }
        return  scell;
    }
    else if (_numOfSection == 3)
    {
        static NSString *identifier = @"secondcell4";
        TypeTableViewCell * scell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!scell) {
            scell = [[TypeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            
            scell.collectionView.dataSource = self;
            scell.collectionView.delegate = self;
        }
        return  scell;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_numOfSection == 0) {
        return  4 * (SCREEN_WIDTH / 4) + (SCREEN_HEIGHT / 10);
    }
    else if (_numOfSection == 1) {
        return 2 * (SCREEN_WIDTH  / 4) + (SCREEN_HEIGHT / 10);
      //  return 300;
 
    }
    else if (_numOfSection == 2) {
        return 2 * (SCREEN_WIDTH  / 4) + (SCREEN_HEIGHT / 10);
       // return 400;
  
    }
    else if (_numOfSection == 3) {
        return 2 * (SCREEN_WIDTH  / 4) + (SCREEN_HEIGHT / 10);

    } else {
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_numOfSection == 0) {
        TypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        [cell drawWithSecation:0 model:_dataSources1[indexPath.row]];
        
        return cell;
    }
    else if (_numOfSection == 1) {
        TypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
  
        [cell drawWithSecation:0 model:_dataSources2[indexPath.row]];
        return cell;
    }
   else if (_numOfSection == 2) {
        TypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];

       [cell drawWithSecation:0 model:_dataSources3[indexPath.row]];
       return cell;
    }
   else if (_numOfSection == 3)
   {
       TypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell3" forIndexPath:indexPath];
     [cell drawWithSecation:0 model:_dataSources4[indexPath.row]];
       return cell;
   }
 
    
    
    
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XiaoKaiAttackViewController * vc = [[XiaoKaiAttackViewController alloc]init];
    if (_numOfSection == 0) {
        vc.myid = [_dataSources1[indexPath.row] myid];
        vc.Push = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (_numOfSection == 1) {
        vc.myid = [_dataSources2[indexPath.row] myid];
        vc.Push = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (_numOfSection == 2) {
        vc.myid = [_dataSources3[indexPath.row] myid];
        vc.Push = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (_numOfSection == 3) {
        vc.myid = [_dataSources4[indexPath.row] myid];
        vc.Push = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
//    [self.navigationController pushViewController:vc animated:YES];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_numOfSection == 0) {
        if (_dataSources1.count == 0) {
            return 0;
        }
        return _dataSources1.count;
    }
   else if (_numOfSection == 1) {
        if (_dataSources2.count == 0) {
            return 0;
        }
        return _dataSources2.count;
    }
   else if (_numOfSection == 2) {
        if (_dataSources3.count == 0) {
            return 0;
        }
        return _dataSources3.count;
    }
   else if (_numOfSection == 3) {
        if (_dataSources4.count == 0) {
            return 0;
        }
        return _dataSources4.count;
    }
    return 0;
}

-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"分类推荐";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH - 50, 25, 32, 32);

    [button setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(sousuo) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
    [self.view addSubview:button];
    [self.view bringSubviewToFront:button];
}
-(void)sousuo
{
    XiaoKaiSearchViewController *seac = [[XiaoKaiSearchViewController alloc]init];
    [self.navigationController pushViewController:seac animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
